// hyb_yl/backstageServices/pages/yearcardpay/yearcardpay.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zid: ""
  },
  createVipOrder() {
    var that = this;
    var money = that.data.zj_year.new_price;
    var openid = wx.getStorageSync('openid');
    var wz_num = that.data.zj_year.wz_num;
    var wz_zhekou = that.data.zj_year.wz_zhekou;
    var jd_num = that.data.zj_year.jd_num;
    var hh_num = that.data.zj_year.hh_num;
    var zid = that.data.zid;
    var yid = that.data.zj_year.id;
    var times = that.data.zj_year.times;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.is_yearcard",
      data: {
        zid: zid,
        yid: yid,
        openid:wx.getStorageSync('openid')
      },
      success: function (res) {
        if (res.data.code == '1') {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            success: function (res) {
              wx.switchTab({
                url: '/hyb_yl/tabBar/index/index',
              })
            }
          })
        } else if (res.data.code == '2') {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            success: function (res) {
              wx.reLaunch({
                url: '/hyb_yl/tabBar/index/index',
              })
            }
          })
        } else if (res.data.code == '3') {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            success: function (con) {
              if(con.confirm)
              {
                app.util.request({
                  'url': "entry/wxapp/zhuanjia.user_blyear",
                  data: {
                    zid: zid,
                    yid: yid,
                    openid: openid,
                    money: money,
                    wz_num: wz_num,
                    wz_zhekou: wz_zhekou,
                    jd_num: jd_num,
                    hh_num: hh_num,
                    times: times
                  },
                  success: function (res) {
                    var created = res.data.created;
                    if (money > '0.00' && money != '') {
                      app.util.request({
                        'url': "entry/wxapp/zhuanjia.pay_year",
                        header: {
                          "Content-Type": "application/xml"
                        },
                        method: "GET",
                        data: {
                          openid: openid,
                          created: created,
                          money: money
                        },
                        success: function (res) {
                          wx.requestPayment({
                            'timeStamp': res.data.timeStamp,
                            'nonceStr': res.data.nonceStr, //字符串随机数
                            'package': res.data.package,
                            'signType': res.data.signType,
                            'paySign': res.data.paySign,
                            success: function (res) {
                              wx.showModal({
                                title: '提示',
                                content: "办理成功",
                                success: function (con) {
                                  if (con.confirm) {
                                    wx.switchTab({
                                      url: '/hyb_yl/tabBar/index/index',
                                    })
                                  }
                                }
                              })
                            },
                            fail: function (res) {
                              console.log(res)
                            }
                          })
                        }
                      })
                    }else{
                      wx.showModal({
                        title: '提示',
                        content: "办理成功",
                        success: function (con) {
                          if (con.confirm) {
                            wx.reLaunch({
                              url: '/hyb_yl/tabBar/index/index',
                            })
                          }
                        }
                      })
                    }
  
                  }
                })
              }
              
            }
          })
        }else{
          app.util.request({
            'url': "entry/wxapp/zhuanjia.user_blyear",
            data: {
              zid: zid,
              yid: yid,
              openid: openid,
              money: money,
              wz_num: wz_num,
              wz_zhekou: wz_zhekou,
              jd_num: jd_num,
              hh_num: hh_num,
              times: times
            },
            success: function (res) {
              var ordersn = res.data.ordersn;
              if (money > '0.00' && money != '') {
                app.util.request({
                  'url': "entry/wxapp/zhuanjia.pay_year",
                  header: {
                    "Content-Type": "application/xml"
                  },
                  method: "GET",
                  data: {
                    openid: openid,
                    ordersn: ordersn,
                    money: money
                  },
                  success: function (res) {
                    wx.requestPayment({
                      'timeStamp': res.data.timeStamp,
                      'nonceStr': res.data.nonceStr, //字符串随机数
                      'package': res.data.package,
                      'signType': res.data.signType,
                      'paySign': res.data.paySign,
                      success: function (res) {
                        app.util.request({
                          'url': 'entry/wxapp/tuike.uptopmoney',
                          data: {
                            tkid: tkid,
                            mytkid: mytkid,
                            money: money,
                            openid: wx.getStorageSync('openid'),
                            leixing: 'yearcard',
                            orders: ordersn
                          },
                          success: function (res) {
                            console.log(res)
                          }
                        })
                        app.util.request({
                          'url':"entry/wxapp/zhuanjia.update_pay",
                          data:{
                            ordersn:ordersn,
                            openid:openid,
                            yid:yid
                          },
                          success:function(res){
                            wx.showModal({
                              title: '提示',
                              content: "办理成功",
                              success: function (con) {
                                if (con.confirm) {
                                  wx.switchTab({
                                    url: '/hyb_yl/tabBar/index/index',
                                  })
                                }
                              }
                            })
                          }
                        })
                        
                      },
                      fail: function (res) {
                        console.log(res)
                      }
                    })
                  }
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: "办理成功",
                  success: function (con) {
                    if (con.confirm) {
                      wx.reLaunch({
                        url: '/hyb_yl/tabBar/index/index',
                      })
                    }
                  }
                })
              }

            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: "#000000",
      backgroundColor: "#ffffff",
    })
    var zid = options.zid;
    this.setData({
      zid: zid,
    })
    this.getzhuanjia_year();
  },
  getzhuanjia_year: function (e) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.zhuanjia_year",
      data: {
        openid: wx.getStorageSync('openid'),
        zid: that.data.zid
      },
      success: function (res) {
        that.setData({
          zj_year: res.data
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})